using System;
using System.Collections.Generic;
using System.Text;
using Entities;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace DAL
{
    public class tbLoaiSanPhamDAL
    {
        private SqlConnection cn = null;
        public tbLoaiSanPhamDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbLoaiSanPhamEntities loaiSP)
        {
            SqlCommand cm = new SqlCommand("sp_tbLoaiSanPham_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenLoai", loaiSP.TenLoai);
            cm.ExecuteNonQuery();
        }
        public void Update(tbLoaiSanPhamEntities loaiSP)
        {
            SqlCommand cm = new SqlCommand("sp_tbLoaiSanPham_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaLoai", loaiSP.MaLoai);
            cm.Parameters.AddWithValue("@TenLoai", loaiSP.TenLoai);
            cm.ExecuteNonQuery();
        }
        public void Delete(int ID)
        {
            SqlCommand cm = new SqlCommand("sp_tbLoaiSanPham_Delete", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaLoai", ID);
            cm.ExecuteNonQuery();
        }
        public tbLoaiSanPhamEntities.tbLoaiSanPhamEntitiesCollection Get(string where, int pageIndex,
            int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_tbLoaiSanPham_GET", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection",
            orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", System.Data.DbType.Int32)).Direction = System.Data.ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbLoaiSanPhamEntities.tbLoaiSanPhamEntitiesCollection LoaiSanPhamCollection = new tbLoaiSanPhamEntities.tbLoaiSanPhamEntitiesCollection();
            while (dr.Read())
            {
                tbLoaiSanPhamEntities loaiSP = new tbLoaiSanPhamEntities();
                loaiSP.MaLoai = int.Parse(dr.GetValue(0).ToString());
                loaiSP.TenLoai = dr.GetValue(1).ToString();
                LoaiSanPhamCollection.Add(loaiSP);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return LoaiSanPhamCollection;
        }
    }
}
